package com.zjt.swipeview.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.zjt.swipeview.SwipeView;
import com.zjt.swipeview.component.SwipeLayout;
import com.zjt.swipeview.event.OnLoadMoreListener;
import com.zjt.swipeview.event.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

public class MainActivity extends AppCompatActivity implements OnRefreshListener, OnLoadMoreListener {

    private static final int PAGE_SIZE = 15;
    private SampleAdapter mAdapter;
    private SwipeView mSwipeView;
    private RecyclerView recyclerView;
    private int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        recyclerView = (RecyclerView) this.findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        mAdapter = new SampleAdapter(this, getData(PAGE_SIZE, index));
        addIndex();
        recyclerView.setAdapter(mAdapter);

        initSwipeView();
    }

    private void initSwipeView(){
        //构建SwipeView
        mSwipeView = SwipeView.with(recyclerView)
                .onRefresh(this)
                .onLoadMore(this)
                .autoLoadMore(true)
                .refreshView(R.layout.load_refresh)
                .loadMoreView(R.layout.load_more)
                .style(SwipeLayout.STYLE.CLASSIC)
                .build();
    }

    private List<String> getData(int num, int offest) {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            data.add("Item " + (i + offest + 1));
        }
        return data;
    }

    private int addIndex() {
        int oldIndex = index;
        index += PAGE_SIZE;
        return oldIndex;
    }

    private int cleanIndex() {
        index = 0;
        return 0;
    }

    @Override
    public void onRefresh() {
        Observable.timer(1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        mAdapter.cleanData();
                        mAdapter.addData(getData(PAGE_SIZE, cleanIndex()));
                        addIndex();
                        //刷新停止
                        mSwipeView.setRefreshing(false);
                    }
                });
    }

    @Override
    public void onLoadMore() {
        Observable.timer(1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        mAdapter.addData(getData(PAGE_SIZE, addIndex()));
                        //加载停止
                        mSwipeView.setLoadingMore(false);
                    }
                });
    }
}
